<?php defined('SYSPATH') or die('No direct script access.');

class Contact_Controller extends  Template_Controller {
	// Disable this controller when Kohana is set to production mode.
	// See http://docs.kohanaphp.com/installation/deployment for more details.
	const ALLOW_PRODUCTION = FALSE;

	// Set the name of the template to use
	public $template = 'car2club/template';
	
	
	public function index(){
		// Look at the counters for valid and invalid
		// responses in the Session Profiler.
		//new Profiler;
		
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('contact');
		
		$this->template->title = 'ติดต่อเรา/Contact Us.';
		$this->template->content = View::factory('contact')
			->bind('form', $form)
			->bind('captcha', $captcha)
			->bind('errors', $errors);

		$form = array(
			'name' => '',
			'email' => '',
			'message' => '',
			'captcha' => '',
		);
		$captcha = Captcha::factory();
		$errors = $form;

		$post = Validation::factory($_POST)
			->pre_filter('trim', TRUE)
			->add_rules('name','required')
			->add_rules('email', 'required', 'valid::email')
			->add_rules('message', 'required')
			->add_rules('captcha', 'required', 'Captcha::valid');
		$this->template->content->success="";
		if ( ! $post->validate())
		{
			// Repopulate the form
			$form = $post->as_array();
			// And use your custom errors messages file
			$errors = $post->errors('custom_error');
		}
		else
		{
			// Do your stuff here. Send your mail.
			$this->template->content->success = '<p>Passed Validation</p>';
			echo 'name '.$post->name.'<br>';
			echo 'email '.$post->email.'<br>';
			echo 'message '.$post->message.'<br>';
			echo 'captcha '.$post->captcha.'<br>';
			url::redirect('contact/insertData');
		}
		


		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
		//$this->template->content->render(true);
	}
	public function insertData(){
		// Look at the counters for valid and invalid
		// responses in the Session Profiler.
		//new Profiler;
		
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('contact_test');
		$this->template->title = 'Test View';
		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
		//$this->template->content->render(true);
	}
}
